//
//  CommitDataViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/31.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CommitDataViewController.h"
#import "CommitDataCell.h"
#import "CommitDataBottomCell.h"
#import "AddressManagerViewController.h"
#import "SignUpInformationViewController.h"
@interface CommitDataViewController ()<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate,UIImagePickerControllerDelegate,UIViewControllerTransitioningDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *titlesArra;
@property(nonatomic,strong)UIImageView *centerImageview;
@property(nonatomic,strong)UIActionSheet *actionSheet;
@end

@implementation CommitDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写报名信息";
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI{
//图片的上部分
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"上传比赛作品";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(140));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(74));
    }];
    UIImageView *leftView = [[UIImageView alloc] init];
    [self.view addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(125));
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(81.5));
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    UIImageView *rightView = [[UIImageView alloc] init];
    [self.view addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-125));
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(81.5));
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    _centerImageview = [[UIImageView alloc] init];
    [self.view addSubview:_centerImageview];
    [_centerImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleLeftAndRight(10));
        make.height.mas_equalTo(scaleTopAndBottom(175));
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoPickPicture:)];
    [_centerImageview addGestureRecognizer:tap];
    _centerImageview.userInteractionEnabled = YES;
//图片的下部分
    UILabel *titleLabel1 = [[UILabel alloc] init];
    titleLabel1.text = @"填写报名信息";
    titleLabel1.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel1];
    [titleLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(140));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.top.equalTo(_centerImageview.mas_bottom).offset(scaleTopAndBottom(10));
    }];
    UIImageView *leftView1 = [[UIImageView alloc] init];
    [self.view addSubview:leftView1];
    [leftView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(125));
        make.top.equalTo(_centerImageview.mas_bottom).offset(scaleTopAndBottom(17.5));
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    UIImageView *rightView1 = [[UIImageView alloc] init];
    [self.view addSubview:rightView1];
    [rightView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-125));
        make.top.equalTo(_centerImageview.mas_bottom).offset(scaleTopAndBottom(17.5));
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(titleLabel1.mas_bottom).offset(scaleTopAndBottom(0));
        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-100));
    }];
    self.tableView.scrollEnabled = NO;
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = view;
    [self.tableView registerClass:[CommitDataCell class] forCellReuseIdentifier:@"CommitDataCell"];
    [self.tableView registerClass:[CommitDataBottomCell class] forCellReuseIdentifier:@"CommitDataBottomCell"];
    leftView.backgroundColor = [UIColor redColor];
    rightView.backgroundColor = [UIColor redColor];
    leftView1.backgroundColor = [UIColor redColor];
    rightView1.backgroundColor = [UIColor redColor];
    _centerImageview.backgroundColor = [UIColor orangeColor];
    UIImageView *chooesImageView = [[UIImageView alloc] init];
    chooesImageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:chooesImageView];
    [chooesImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(15));
        make.top.equalTo(self.tableView.mas_bottom).offset(scaleTopAndBottom(20));
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    UILabel *readLabel = [[UILabel alloc] init];
    [self.view addSubview:readLabel];
    readLabel.text = @"我已阅读并同意";
    readLabel.adjustsFontSizeToFitWidth = YES;
    readLabel.textAlignment = NSTextAlignmentCenter;
    [readLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chooesImageView.mas_right).offset(scaleLeftAndRight(0));
        make.top.equalTo(self.tableView.mas_bottom).offset(scaleTopAndBottom(20));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(120), scaleTopAndBottom(15)));
    }];
    UIButton *introdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    introdBtn.backgroundColor = [UIColor redColor];
    introdBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [introdBtn setTitle:@"免责声明" forState:UIControlStateNormal];
    [self.view addSubview:introdBtn];
    [introdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(readLabel.mas_right).offset(scaleLeftAndRight(0));
        make.top.equalTo(self.tableView.mas_bottom).offset(scaleTopAndBottom(20));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(80), scaleTopAndBottom(15)));
    }];
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.backgroundColor = [UIColor redColor];
    [commitBtn setTitle:@"提交资料" forState:UIControlStateNormal];
    commitBtn.layer.cornerRadius = scaleTopAndBottom(20);
    commitBtn.layer.masksToBounds = YES;
    [commitBtn addTarget:self action:@selector(goToSignupInformationPage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(15));
        make.top.equalTo(readLabel.mas_bottom).offset(scaleTopAndBottom(20));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-15));
        make.height.mas_equalTo(scaleTopAndBottom(40));
    }];
}
#pragma mark -tablview的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.titlesArra.count;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CommitDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommitDataCell"];
        cell.labell.text = self.titlesArra[indexPath.row];
        return cell;
    }else{
        CommitDataBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommitDataBottomCell"];
        return cell;
    }


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return scaleTopAndBottom(45);
    }else{
        return scaleTopAndBottom(60);
    }
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
    }else{
        AddressManagerViewController *addressVc = [[AddressManagerViewController alloc] init];
        [self.navigationController pushViewController:addressVc animated:YES];
    }
}
-(NSArray *)titlesArra{
    if (_titlesArra == nil) {
        _titlesArra = @[@"作品名称",@"宝宝姓名",@"指导老师",@"推选机构"];
        
    }
    return _titlesArra;
}

#pragma mark -点击上传营业执照图片的手势
-(void)gotoPickPicture:(UITapGestureRecognizer *)tap{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择",  nil];
    }else{
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    
    self.actionSheet.tag = 10000;
    [self.actionSheet showInView:self.view];
    
}
#pragma mark - 调用相机的代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 10000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //来源:相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //来源:相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        imagePickerController.transitioningDelegate = self;
        imagePickerController.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}
//报名信息页面的点击事件
-(void)goToSignupInformationPage:(UIButton *)btn{
    SignUpInformationViewController *signInforVc = [[SignUpInformationViewController alloc] init];
    [self.navigationController pushViewController:signInforVc animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    self.centerImageview.image = image;
    [self.centerImageview setContentScaleFactor:[[UIScreen mainScreen] scale]];
    self.centerImageview.contentMode =  UIViewContentModeScaleAspectFill;
    self.centerImageview.clipsToBounds  = YES;
    
}
//缩放图片到指定大小的imageview
- (UIImage*)imageCompressWithSimple:(UIImage*)image scaledToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
